/***************************************************************************
 *   Copyright (C) 2009 by p1r0                                            *
 *   lucifers.preacher@gmail.com                                           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "auxiliary.h"
#include <string>
#include <iostream>
#include <curses.h>

#include "constants.h"
#include "conf.h"

void exposeArgs(int argc, char* argv[])
{
    bool first = true;
    string source = "var argv = new Array(";
    for(int i = 1; i < argc; i++)
    {
        if(first)
        {
            first = false;
        }
        else
        {
            source += ", ";
        }
        source += "'" + string(argv[i]) + "'";
    }
    source += ");";

    Handle<String> v8Source = String::New(source.c_str());
    Handle<Script> script = Script::Compile(v8Source);
    Handle<Value> result = script->Run();
}

void displayV8Error(v8::TryCatch* try_catch) 
{
    endwin(); //Make sure curses win is ended

    cout << "There is an error in the script." << endl << endl;

    v8::HandleScope handle_scope;
    v8::String::Utf8Value exception(try_catch->Exception());
    string exception_string = string(*exception);
    v8::Handle<v8::Message> message = try_catch->Message();
    if (message.IsEmpty()) 
    {
        cout << exception_string << endl;
    } 
    else 
    {
        v8::String::Utf8Value filename(message->GetScriptResourceName());
        string filename_string = string(*filename);
        int linenum = message->GetLineNumber();
        cout << filename_string << ":" << linenum << " " << exception_string << endl;
        // Print line of source code.
        v8::String::Utf8Value sourceline(message->GetSourceLine());
        string sourceline_string = string(*sourceline);
        cout << sourceline_string << endl;
        // Print wavy underline (GetUnderline is deprecated).
        int start = message->GetStartColumn();
        for (int i = 0; i < start; i++) 
        {
            printf(" ");
        }
        int end = message->GetEndColumn();
        for (int i = start; i < end; i++) 
        {
            printf("^");
        }

        printf("\n");
    }
}

bool parseArgOptions(int argc, char* argv[])
{
    bool ret = true;

    for(int i = 1; i < argc; i++)
    {
        if(argv[i][0] == '-')
        {
            //This argument contains options, parse it.
            for(int j = 1; j < strlen(argv[i]); j++)
            {
                bool retProc = procOption(argv[i][j]);
                ret = ret && retProc;
            }
        }
    }

    return ret;
}

bool procOption(char option)
{
    bool ret = true;

    switch(option)
    {
        case 'a':
            ret = showAbout();
            break;
        case 'h':
            ret = showHelp();
            break;
        case 'j':
            Conf::getInstance()->isJailed(true);
            ret = true;
            break;
        default:
            ret = true;
            break;
    }
    
    return ret;
}

bool showAbout()
{
    cout << endl << __APP_LIC_NOTICE__ << endl;
    return false;
}

bool showHelp()
{
    cout << __APP_NAME__ << " version: " << __APP_VERSION__ << endl << endl;
    cout << "HELP" << endl << endl;

    cout << "Usage: jssh [OPTION...] [SCRIPT]" << endl << endl;
    cout << "AVAILABLE OPTIONS: " << endl;
    cout << "\t-a\tShow about info" << endl;
    cout << "\t-h\tShow this help" << endl;
    cout << "\t-j\tRun your script in (J)ail mode. This means that the script will" << endl;
    cout <<     "\t\tnot have access to any function that gives direct access to the system." << endl << endl;
    
    cout << "NOTE: " << endl;
    cout << "\tUnexisting options will be omitten and no warning or error will shown." << endl;
    cout << endl << endl;

    return false;
}
